<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic\task;


use app\common\model\skits\Skits;
use app\common\model\task\Task;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * Task逻辑
 * Class TaskLogic
 * @package app\adminapi\logic
 */
class TaskLogic extends BaseLogic
{

    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/03/02 17:16
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            Task::where('id', $params['id'])->update([
                'z_coin'   => $params['z_coin'],
                'schedule' => $params['schedule']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }

    /**
     * @notes 获取任务配置
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/01/17 14:07
     */
    public static function config($params): array
    {
        $result                = [];
        $result['novice_list'] = Task::where('app_id', $params['app_id'])->where('type', 1)->field(['id', 'name', 'z_coin', 'key', 'schedule'])->order('id asc')->select()->toArray();
        $result['daily_list']  = Task::where('app_id', $params['app_id'])->where('type', 2)->field(['id', 'name', 'z_coin', 'key', 'schedule'])->order('id asc')->select()->toArray();
        foreach ($result['novice_list'] as &$item) {
            $item['name'] = str_replace('{$schedule}', $item['schedule'], $item['name']);
        }
        foreach ($result['daily_list'] as &$item) {
            $item['name'] = str_replace('{$schedule}', $item['schedule'], $item['name']);
        }
        return $result;
    }


    /**
     * 复制平台任务
     *
     * @param $appId
     * @return bool|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function copyAppTask($appId)
    {
        $list = Task::where('app_id', '=', 1)->order('id', 'asc')->select();
        if ($list->isEmpty()) {

            return false;
        }
        $insertData = [];
        foreach ($list as $item) {
            array_push($insertData, [
                'app_id'      => $appId,
                'key'         => $item['key'] ?? '',
                'name'        => $item['name'] ?? '',
                'description' => $item['description'] ?? '',
                'z_coin'      => $item['z_coin'] ?? '',
                'status'      => $item['status'] ?? '',
                'type'        => $item['type'] ?? '',
                'path'        => $item['path'] ?? '',
                'icon'        => $item['icon'] ?? '',
                //                'schedule'    => $item['schedule'] ?? '',
            ]);
        }

        return Task::insertAll($insertData);
    }
}
